﻿/**  
* Ver    变更日期             负责人  变更内容
* V0.01  2015/11/11 17:10:40   何泉水    初版
*/
using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:article
	/// </summary>
	public partial class article
	{
		public article()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_article"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_article");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.article model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_article(");
			strSql.Append("name,category_id,title,form,serial_num,color,price,seo_title,seo_keywords,seo_desc,wx_keyword,file_path,img_url,img_urls,link_url,content,add_time,click,sort_id,is_top,is_red,is_hot,is_lock,is_sys,attr_str,attr_str2,attr_num)");
			strSql.Append(" values (");
			strSql.Append("@name,@category_id,@title,@form,@serial_num,@color,@price,@seo_title,@seo_keywords,@seo_desc,@wx_keyword,@file_path,@img_url,@img_urls,@link_url,@content,@add_time,@click,@sort_id,@is_top,@is_red,@is_hot,@is_lock,@is_sys,@attr_str,@attr_str2,@attr_num)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@category_id", DbType.Int32,8),
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@form", DbType.String),
					new SQLiteParameter("@serial_num", DbType.String),
					new SQLiteParameter("@color", DbType.String),
					new SQLiteParameter("@price", DbType.Decimal,4),
					new SQLiteParameter("@seo_title", DbType.String),
					new SQLiteParameter("@seo_keywords", DbType.String),
					new SQLiteParameter("@seo_desc", DbType.String),
					new SQLiteParameter("@wx_keyword", DbType.String),
					new SQLiteParameter("@file_path", DbType.String),
					new SQLiteParameter("@img_url", DbType.String),
					new SQLiteParameter("@img_urls", DbType.String),
					new SQLiteParameter("@link_url", DbType.String),
					new SQLiteParameter("@content", DbType.String),
					new SQLiteParameter("@add_time", DbType.String),
					new SQLiteParameter("@click", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@is_top", DbType.Int32,8),
					new SQLiteParameter("@is_red", DbType.Int32,8),
					new SQLiteParameter("@is_hot", DbType.Int32,8),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@is_sys", DbType.Int32,8),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@attr_str2", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.category_id;
			parameters[2].Value = model.title;
			parameters[3].Value = model.form;
			parameters[4].Value = model.serial_num;
			parameters[5].Value = model.color;
			parameters[6].Value = model.price;
			parameters[7].Value = model.seo_title;
			parameters[8].Value = model.seo_keywords;
			parameters[9].Value = model.seo_desc;
			parameters[10].Value = model.wx_keyword;
			parameters[11].Value = model.file_path;
			parameters[12].Value = model.img_url;
			parameters[13].Value = model.img_urls;
			parameters[14].Value = model.link_url;
			parameters[15].Value = model.content;
			parameters[16].Value = model.add_time;
			parameters[17].Value = model.click;
			parameters[18].Value = model.sort_id;
			parameters[19].Value = model.is_top;
			parameters[20].Value = model.is_red;
			parameters[21].Value = model.is_hot;
			parameters[22].Value = model.is_lock;
			parameters[23].Value = model.is_sys;
			parameters[24].Value = model.attr_str;
			parameters[25].Value = model.attr_str2;
			parameters[26].Value = model.attr_num;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.article model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_article set ");
			strSql.Append("name=@name,");
			strSql.Append("category_id=@category_id,");
			strSql.Append("title=@title,");
			strSql.Append("form=@form,");
			strSql.Append("serial_num=@serial_num,");
			strSql.Append("color=@color,");
			strSql.Append("price=@price,");
			strSql.Append("seo_title=@seo_title,");
			strSql.Append("seo_keywords=@seo_keywords,");
			strSql.Append("seo_desc=@seo_desc,");
			strSql.Append("wx_keyword=@wx_keyword,");
			strSql.Append("file_path=@file_path,");
			strSql.Append("img_url=@img_url,");
			strSql.Append("img_urls=@img_urls,");
			strSql.Append("link_url=@link_url,");
			strSql.Append("content=@content,");
			strSql.Append("add_time=@add_time,");
			strSql.Append("click=@click,");
			strSql.Append("sort_id=@sort_id,");
			strSql.Append("is_top=@is_top,");
			strSql.Append("is_red=@is_red,");
			strSql.Append("is_hot=@is_hot,");
			strSql.Append("is_lock=@is_lock,");
			strSql.Append("is_sys=@is_sys,");
			strSql.Append("attr_str=@attr_str,");
			strSql.Append("attr_str2=@attr_str2,");
			strSql.Append("attr_num=@attr_num");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@category_id", DbType.Int32,8),
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@form", DbType.String),
					new SQLiteParameter("@serial_num", DbType.String),
					new SQLiteParameter("@color", DbType.String),
					new SQLiteParameter("@price", DbType.Decimal,4),
					new SQLiteParameter("@seo_title", DbType.String),
					new SQLiteParameter("@seo_keywords", DbType.String),
					new SQLiteParameter("@seo_desc", DbType.String),
					new SQLiteParameter("@wx_keyword", DbType.String),
					new SQLiteParameter("@file_path", DbType.String),
					new SQLiteParameter("@img_url", DbType.String),
					new SQLiteParameter("@img_urls", DbType.String),
					new SQLiteParameter("@link_url", DbType.String),
					new SQLiteParameter("@content", DbType.String),
					new SQLiteParameter("@add_time", DbType.String),
					new SQLiteParameter("@click", DbType.Int32,8),
					new SQLiteParameter("@sort_id", DbType.Int32,8),
					new SQLiteParameter("@is_top", DbType.Int32,8),
					new SQLiteParameter("@is_red", DbType.Int32,8),
					new SQLiteParameter("@is_hot", DbType.Int32,8),
					new SQLiteParameter("@is_lock", DbType.Int32,8),
					new SQLiteParameter("@is_sys", DbType.Int32,8),
					new SQLiteParameter("@attr_str", DbType.String),
					new SQLiteParameter("@attr_str2", DbType.String),
					new SQLiteParameter("@attr_num", DbType.Int32,8),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.category_id;
			parameters[2].Value = model.title;
			parameters[3].Value = model.form;
			parameters[4].Value = model.serial_num;
			parameters[5].Value = model.color;
			parameters[6].Value = model.price;
			parameters[7].Value = model.seo_title;
			parameters[8].Value = model.seo_keywords;
			parameters[9].Value = model.seo_desc;
			parameters[10].Value = model.wx_keyword;
			parameters[11].Value = model.file_path;
			parameters[12].Value = model.img_url;
			parameters[13].Value = model.img_urls;
			parameters[14].Value = model.link_url;
			parameters[15].Value = model.content;
			parameters[16].Value = model.add_time;
			parameters[17].Value = model.click;
			parameters[18].Value = model.sort_id;
			parameters[19].Value = model.is_top;
			parameters[20].Value = model.is_red;
			parameters[21].Value = model.is_hot;
			parameters[22].Value = model.is_lock;
			parameters[23].Value = model.is_sys;
			parameters[24].Value = model.attr_str;
			parameters[25].Value = model.attr_str2;
			parameters[26].Value = model.attr_num;
			parameters[27].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_article ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_article ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.article GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,category_id,title,form,serial_num,color,price,seo_title,seo_keywords,seo_desc,wx_keyword,file_path,img_url,img_urls,link_url,content,add_time,click,sort_id,is_top,is_red,is_hot,is_lock,is_sys,attr_str,attr_str2,attr_num from he_article ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.article model=new He.Model.article();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.article DataRowToModel(DataRow row)
		{
			He.Model.article model=new He.Model.article();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["name"]!=null)
				{
					model.name=row["name"].ToString();
				}
				if(row["category_id"]!=null && row["category_id"].ToString()!="")
				{
					model.category_id=int.Parse(row["category_id"].ToString());
				}
				if(row["title"]!=null)
				{
					model.title=row["title"].ToString();
				}
				if(row["form"]!=null)
				{
					model.form=row["form"].ToString();
				}
				if(row["serial_num"]!=null)
				{
					model.serial_num=row["serial_num"].ToString();
				}
				if(row["color"]!=null)
				{
					model.color=row["color"].ToString();
				}
				if(row["price"]!=null && row["price"].ToString()!="")
				{
					model.price=decimal.Parse(row["price"].ToString());
				}
				if(row["seo_title"]!=null)
				{
					model.seo_title=row["seo_title"].ToString();
				}
				if(row["seo_keywords"]!=null)
				{
					model.seo_keywords=row["seo_keywords"].ToString();
				}
				if(row["seo_desc"]!=null)
				{
					model.seo_desc=row["seo_desc"].ToString();
				}
				if(row["wx_keyword"]!=null)
				{
					model.wx_keyword=row["wx_keyword"].ToString();
				}
				if(row["file_path"]!=null)
				{
					model.file_path=row["file_path"].ToString();
				}
				if(row["img_url"]!=null)
				{
					model.img_url=row["img_url"].ToString();
				}
				if(row["img_urls"]!=null)
				{
					model.img_urls=row["img_urls"].ToString();
				}
				if(row["link_url"]!=null)
				{
					model.link_url=row["link_url"].ToString();
				}
				if(row["content"]!=null)
				{
					model.content=row["content"].ToString();
				}
				if(row["add_time"]!=null)
				{
					model.add_time=row["add_time"].ToString();
				}
				if(row["click"]!=null && row["click"].ToString()!="")
				{
					model.click=int.Parse(row["click"].ToString());
				}
				if(row["sort_id"]!=null && row["sort_id"].ToString()!="")
				{
					model.sort_id=int.Parse(row["sort_id"].ToString());
				}
				if(row["is_top"]!=null && row["is_top"].ToString()!="")
				{
					model.is_top=int.Parse(row["is_top"].ToString());
				}
				if(row["is_red"]!=null && row["is_red"].ToString()!="")
				{
					model.is_red=int.Parse(row["is_red"].ToString());
				}
				if(row["is_hot"]!=null && row["is_hot"].ToString()!="")
				{
					model.is_hot=int.Parse(row["is_hot"].ToString());
				}
				if(row["is_lock"]!=null && row["is_lock"].ToString()!="")
				{
					model.is_lock=int.Parse(row["is_lock"].ToString());
				}
				if(row["is_sys"]!=null && row["is_sys"].ToString()!="")
				{
					model.is_sys=int.Parse(row["is_sys"].ToString());
				}
				if(row["attr_str"]!=null)
				{
					model.attr_str=row["attr_str"].ToString();
				}
				if(row["attr_str2"]!=null)
				{
					model.attr_str2=row["attr_str2"].ToString();
				}
				if(row["attr_num"]!=null && row["attr_num"].ToString()!="")
				{
					model.attr_num=int.Parse(row["attr_num"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,category_id,title,form,serial_num,color,price,seo_title,seo_keywords,seo_desc,wx_keyword,file_path,img_url,img_urls,link_url,content,add_time,click,sort_id,is_top,is_red,is_hot,is_lock,is_sys,attr_str,attr_str2,attr_num ");
			strSql.Append(" FROM he_article ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_article ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from he_article T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_article";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

